package com.ease.core.components {
   import com.ease.core.components.dockableToolBarClasses.DockableToolBarDragStrip;
   import com.ease.core.cursors.EastWestCursor;
   
   import flash.events.MouseEvent;
   import flash.geom.Point;
   
   import mx.containers.HBox;
   import mx.core.DragSource;
   import mx.core.ScrollPolicy;
   import mx.core.UIComponent;
   import mx.core.mx_internal;
   import mx.managers.CursorManager;
   import mx.managers.DragManager;

   use namespace mx_internal;

   public class DockableToolBar extends HBox {
      public function DockableToolBar() {
         super();

         this.styleName = "dockableToolBar";

         horizontalScrollPolicy = ScrollPolicy.OFF;
         verticalScrollPolicy = ScrollPolicy.OFF;
      }

      private var dragStrip:UIComponent;

      /**
       * Flag which indicates whether this ToolBar can be dragged by user.
       */
      public var draggable:Boolean = true;

      /**
       * Flag which indicates whether this ToolBar can be dragged by user.
       */
      public var locked:Boolean = false;

      private var estimatedWidth:Number;

      mx_internal var rowCount:int = -1;

      /**
       *  @private
       *  Horizontal location where the user pressed the mouse button
       *  to start dragging
       */
      private var regX:Number;

      /**
       *  @private
       *  Vertical location where the user pressed the mouse button
       *  to start dragging
       */
      private var regY:Number;

      override public function set height(newHeight:Number):void {
         super.height = newHeight;
      }

      override protected function measure():void {
         super.measure();
         layoutObject.measure();
      }

      override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
         if (dragStrip) {
            dragStrip.x = 0;
            dragStrip.y = 0;
         }
         super.updateDisplayList(unscaledWidth, unscaledHeight);
         layoutObject.updateDisplayList(unscaledWidth, unscaledHeight);
      }

      override protected function createChildren():void {
         if (!locked) {
            if (null == dragStrip) {
               dragStrip = new DockableToolBarDragStrip();
               this.addChildAt(dragStrip, 0);
            }
            dragStrip.addEventListener(MouseEvent.ROLL_OVER, onRollOver);
            dragStrip.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
         }
         super.createChildren();
      }

      private var cursorId:int;

      protected function onRollOver(event:MouseEvent):void {
         if (!event.buttonDown) {
            dragStrip.removeEventListener(MouseEvent.ROLL_OVER, onRollOver);
            dragStrip.addEventListener(MouseEvent.ROLL_OUT, onRollOut);
            cursorId = CursorManager.setCursor(EastWestCursor);
         }
      }

      protected function onRollOut(event:MouseEvent):void {
         if (!event.buttonDown) {
            dragStrip.removeEventListener(MouseEvent.ROLL_OUT, onRollOut);
            dragStrip.addEventListener(MouseEvent.ROLL_OVER, onRollOver);
            CursorManager.removeCursor(cursorId);
         }
      }

      override public function getExplicitOrMeasuredWidth():Number {
         return Math.min(systemManager.screen.width, super.getExplicitOrMeasuredWidth());
      }

      protected function onMouseDown(event:MouseEvent):void {
         CursorManager.removeAllCursors();

         var point:Point = this.globalToLocal(new Point(event.stageX, event.stageY));
         regX = point.x;
         regY = point.y;

         dragStrip.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);

         if (measuredWidth > 200 || !isNaN(percentWidth)) {
            var area:int = measuredHeight * measuredWidth;
            estimatedWidth = Math.max(Math.sqrt(area) * 1.6, measuredMinWidth);
         }
         else
            estimatedWidth = measuredWidth;
      }

      protected function onMouseMove(event:MouseEvent):void {

         if (event.buttonDown == true) {
            var ds:DragSource = new DragSource();
            ds.addData(event.target, "dragStrip");

            DragManager.doDrag(UIComponent(UIComponent(event.target).parent), ds, event, null, 0, 0, 0);
         }
      }

   }
}